﻿#if !DESKTOP_TARGET

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework; //for Vector3 struct definition

namespace gamecontrol
{
    public class InputAccelerometer : InputBase
    {
        Accelerometer accelerometer;
        bool isAccelDataValid = false;
        Vector3 acceleration;

        public InputAccelerometer(FrameworkElement elem) :
            base(elem)
        {}

        public override void handleOnLoaded(object sender, EventArgs e)
        {
            accelerometer = new Accelerometer();
            accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
            accelerometer.CurrentValueChanged += accelerometerDataChanged;
            accelerometer.Start();
        }

        void accelerometerDataChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            // Note that this event handler is called from a background thread
            // and therefore does not have access to the UI thread. To update 
            // the UI from this handler, use Dispatcher.BeginInvoke() as shown.
            // Dispatcher.BeginInvoke(() => { statusTextBlock.Text = "in CurrentValueChanged"; });

            isAccelDataValid = accelerometer.IsDataValid;
            acceleration = e.SensorReading.Acceleration;
            if (isAccelDataValid)
                calculateDelta((int)(acceleration.X * 100));
        }
    }
}
#endif
